package com.cxd.base.util.pdf;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.cxd.ancun.entity.base.ContractBean;
import com.cxd.base.util.ProjecConfig;
import com.cxd.base.util.wtj.FUtils;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * <一句话功能简述> <功能详细描述>
 * @author wtj
 * @date 2016年4月8日 下午3:44:27
 * @company CXD
 * @version [Copyright (c) 2016 V001]
 */
public class PDFUtils {
	
	public static void createContractHtml(Map<String,Object> root,ContractBean cb) throws IOException, TemplateException, com.lowagie.text.DocumentException{
		/**
		 * E:\\git\\ancunImpl\\src\\main\\webapp
		 * ProjecConfig.PROJECT_BASE_PATH; 
		 */
		String basePath;
		if(cb.getProjectBasePath()==null){
			basePath = ProjecConfig.BASE_FIlE_PATH;
		}else{
			basePath = cb.getProjectBasePath();
		}
		String templatePath = basePath+"public/template/";
		/* 创建配置 */
		@SuppressWarnings("deprecation")
		Configuration cfg = new Configuration();
		
		/* 指定模板存放的路径 */
		cfg.setDirectoryForTemplateLoading(new File(templatePath));
		cfg.setDefaultEncoding("UTF-8");
		
		/* 从上面指定的模板目录中加载对应的模板文件 */
		Template temp;
		if(cb.getContractType().equals("quartet_contract8")){
			temp = cfg.getTemplate("service_contract.shtml");
		}else if(cb.getContractType().equals("quartet_contract")){
			temp = cfg.getTemplate("contract.shtml");
		}else{
			temp = cfg.getTemplate("contract_template.html");
		}
		cfg.setClassicCompatible(true);		/* 创建数据模型 */
		
		/* 将生成的内容写入html中 */
		String htmlPath = basePath + String.format("public/resources/contract/html/%s.html",cb.getInvestId());
		File htmlFile = new File(htmlPath);
		if (!htmlFile.exists())
			htmlFile.createNewFile();
		Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "utf-8"));

		// 模板参数
		root.put("webRoot", ProjecConfig.WEB_ROOT);
		temp.process(root, out);
		temp.setOutputEncoding("utf-8");
		out.flush();
		String pdfName = cb.getInvestId()+".pdf";
		String pdfDirPath = basePath + String.format("public/resources/contract/pdf/");
		PDFUtils.htmlToPdf(pdfDirPath, htmlPath, pdfName,templatePath);
		
//		cb.setPdfUrl(pdfDirPath);
		cb.setPdfUrl(ProjecConfig.WEB_ROOT+String.format("/public/resources/contract/pdf/%s",pdfName));
//		cb.setPdfUrl(htmlPath);
	}
	
	/**
	 * html 转 pdf
	 * @author wtj
	 * @date 2016年4月12日下午1:28:41
	 * @param pdfDirPath pdf生成目录
	 * @param htmlPath	html 生成目录
	 * @param pdfName	pdf文件名称
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static void htmlToPdf(String pdfDirPath,String htmlPath,String pdfName,String templatePath) throws DocumentException, IOException{
//		String pdfPath = pdfBasePath + String.format("/public/resources/contract/pdf/%s.pdf",pdfName);
		String pafPath = pdfDirPath + pdfName;
		System.out.println(pafPath);
		OutputStream os = new FileOutputStream(pafPath);

		ITextRenderer renderer = new ITextRenderer();
		renderer.setDocumentFromString(FUtils.readTxtFile(htmlPath));
		// 解决中文问题
		ITextFontResolver fontResolver = renderer.getFontResolver();
		System.out.println(templatePath+"/simsun.ttc");
		fontResolver.addFont(templatePath+"/simsun.ttc",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

		renderer.layout();
		renderer.createPDF(os);
		os.close();
	}
	
	
	public static void main(String[] arg) throws IOException, TemplateException, com.lowagie.text.DocumentException{
		ContractBean cb = new ContractBean();
//		cb.setInvestId("123456");
//		cb.setUserId("wlsnow");
//		cb.setContractType("test");
//		cb.setProjectBasePath("E:\\git\\ancunImpl\\src\\main\\webapp\\");
		
		
//		String pdfDirPath = "E:/git/ancunImpl/src/main/webapp/public/resources/contract/";
//		String htmlPath = "E:/git/ancunImpl/src/main/webapp/public/resources/contract/test/抵押借款.html";
//		String templatePath = "E:/git/ancunImpl/src/main/webapp/public/template";
//		PDFUtils.htmlToPdf(pdfDirPath,htmlPath,"抵押借款.pdf",templatePath);
		
		String pdfDirPath = "E:/git/ancunImpl/src/main/webapp/public/resources/contract/";
		String htmlPath = "E:/git/ancunImpl/src/main/webapp/public/resources/contract/test/理财计划.html";
		String templatePath = "E:/git/ancunImpl/src/main/webapp/public/template";
		PDFUtils.htmlToPdf(pdfDirPath,htmlPath,"理财计划.pdf",templatePath);
	}
}
